﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using LogLib;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ComLib.ComFun
{
    public static class Serializer
    {
        /// <summary>
        /// 暴露给外部的通知信息
        /// </summary>
        private static string _strMessage = "";
        public static string Message
        {
            get
            {
                return _strMessage;
            }
        }

        /// <summary>
        /// 同步写锁
        /// </summary>
        private static object lockflag = new object();
       
        public static bool SaveToXml(string filePath, object sourceObj)
        {
            lock (lockflag)
            {
                if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
                {
                    Type type = sourceObj.GetType();
                    using (StreamWriter writer = new StreamWriter(filePath, false))
                    {
                        try
                        {
                            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                            xmlSerializer.Serialize(writer, sourceObj);
                            return true;
                        }
                        catch (Exception ex)
                        {
                            _strMessage = "序列化过程发生了异常: " + ex.Message;
                            Log.WriteException(ex.ToString());
                            return false;
                        }
                    }
                }
                else
                {
                    _strMessage = "序列化失败: 路径或对象为空.";
                    return false;
                }
            }         
        }

        public static object LoadFromXml(string filePath, Type type)
        {
            object result = null;
            if (File.Exists(filePath))
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    try
                    {
                        System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                        result = xmlSerializer.Deserialize(reader);
                    }
                    catch (Exception ex)
                    {
                        _strMessage = ex.Message;
                        Log.WriteException(ex.ToString());
                        return null;
                    }
                }
            }
            else 
            {
                _strMessage = "找不到配置文件: " + filePath;
            }
            return result;
        }

        public static bool SaveToDatFile(string filePath, object sourceObj)
        {
            lock (lockflag)
            {
                if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
                {
                    Type type = sourceObj.GetType();
                    using (Stream writer = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        try
                        {
                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(writer, sourceObj);
                            return true;
                        }
                        catch (Exception ex)
                        {
                            _strMessage = "序列化过程发生了异常: " + ex.Message;
                            Log.WriteException(ex.ToString());
                            return false;
                        }
                    }
                }
                else
                {
                    _strMessage = "序列化失败: 路径或对象为空.";
                    return false;
                }
            }
        }

        public static object LoadFromDatFile(string filePath)
        {
            object result = null;
            if (File.Exists(filePath))
            {
                using (Stream destream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    try
                    {
                        IFormatter formatter = new BinaryFormatter();
                        result = formatter.Deserialize(destream);
                    }
                    catch (Exception ex)
                    {
                        _strMessage = ex.ToString();
                        Log.WriteException(ex.ToString());
                        return null;
                    }
                }
            }
            else
            {
                _strMessage = "找不到配置文件: " + filePath;
            }
            return result;
        }

    }
}
